//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-4-14
// @Time:下午2:28
//
#include <iostream>
#include <sstream>
#include "Business/DataBase/DataBaseHelper.h"
#include "Tools/CommonTool.h"

#include <chrono>

#define DBNAME "test.db"

int64_t getTimeMicro() {
    std::chrono::time_point<std::chrono::system_clock> p = std::chrono::system_clock::now();
    return std::chrono::duration_cast<std::chrono::microseconds>(p.time_since_epoch()).count();
}

int main() {
    DataBaseHelper dataBaseHelper(DATABASENAME);

    dataBaseHelper.tableCheckExists();

    std::string username = "dada";
    std::string password = "dasdad";
    std::string email = "dadademial";
    dataBaseHelper.getUserDataBaseFactory()->addUser(username, password, email);

    int ret = dataBaseHelper.getUserDataBaseFactory()->checktUser(username, password, "");
    std::cout << " ret = " << ret << std::endl;

    std::string newpws = "newpws";
    ret = dataBaseHelper.getUserDataBaseFactory()->updataPassword(username, password, newpws);
    std::cout << " ret = " << ret << std::endl;

//    SensorDataType dataType{1,2,3,4,5,"dda11"};
//    SensorDataType dataType2{1,2,3,4,6,"dda12"};
//
//    dataBaseHelper.getIoTDataBaseFactory()->insertData(dataType);
//    dataBaseHelper.getIoTDataBaseFactory()->insertData(dataType2);


    for (int j = 0; j < 100 ; ++j)
    {
        std::stringstream ss;
        ss<<"data "<< j ;
        SensorDataType dataType2{1,1,4,4,j,StringToHex(ss.str())};

        dataBaseHelper.getIoTDataBaseFactory()->insertData(dataType2);
    }

    std::vector<SensorDataType> datas;


//    dataBaseHelper.getIoTDataBaseFactory()->findDataByUid(datas , 1 , 2,3,4,0 ,10 , 2,getTimeMicro());

    dataBaseHelper.getIoTDataBaseFactory()->findDataByPublic(datas, 2, 3, 10, 1, getTimeMicro());
    for (int i = 0; i < datas.size(); ++i) {
        std::cout << "======================" << i << "======" << std::endl;
        std::cout << " data " << i << std::endl;
        std::cout << " uid " << datas.at(i).uid << std::endl;
        std::cout << " data " << datas.at(i).data << std::endl;
        std::cout << " key " << datas.at(i).key << std::endl;
        std::cout << " DevideId " << datas.at(i).DeviceId << std::endl;
    }
//    dataBaseHelper.getUserDataBaseFactory()->insertUser();

    std::vector<uint64_t> gids;
    dataBaseHelper.getIoTDataBaseFactory()->addOrFindGatewayId(6, gids);
    for (auto gid : gids) {
        std::cout << " gid = " << gid << std::endl;
    }
    std::string note = "";
    uint64_t out = 0;
//    dataBaseHelper.getIoTDataBaseFactory()->addDevice(1, 2 ,3 ,note,out);
    std::vector<DeviceDataType> devices;
    dataBaseHelper.getIoTDataBaseFactory()->findDevices(devices, 1, 2, 2);
    std::cout << " out = " << out << std::endl;
    for (auto did : devices) {
        std::cout << " did = " << did.DevideId<< std::endl;
        std::cout << " Token = " << did.Token<< std::endl;
        std::cout << " Note = " << did.Note<< std::endl;
        std::cout << " create = " << did.create<< std::endl;
    }

    std::string token = "daadadadasd";
    dataBaseHelper.getIoTDataBaseFactory()->loginDeviceByToken(token);


}